package cn.tedu.springmvc.controller;

import cn.tedu.springmvc.util.JsonResult;
import cn.tedu.springmvc.vo.UserVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController // 必须是@Controller，不可以是其它组件注解
@RequestMapping("/user")
public class UserController {

    public UserController() {
        System.out.println("UserController.UserController()");
    }

    // http://localhost:8080/springmvc01_war_exploded/login.do
    @RequestMapping("/login.do")
    public String login() {
        return "111";
    }

    @GetMapping("/info.do")
    public UserVO info() {
        UserVO userVO = new UserVO();
        userVO.setUsername("chengheng");
        userVO.setPassword("1234567890");
        userVO.setEmail("chengheng@qq.com");
        return userVO;
    }

    @RequestMapping("/reg.do")
    public String reg(String username, String password, Integer age) {
        System.out.println("username = " + username
                + ", password = " + password
                + ", age = " + age);
        return "OK";
    }
    @GetMapping("/{id:[0-9]+}/info.do")
    public JsonResult<UserVO> info(@PathVariable Long id){
        System.out.println("即将查询id="+id+"的用户的信息..");
        UserVO userVO = new UserVO();
        userVO.setUsername("zhaowang");
        userVO.setPassword("123456");
        userVO.setEmail("zhaowang@qq.com");
        return JsonResult.ok(userVO);
    }
    @GetMapping("/{username:[a-zA-Z]+}/info.do")
    public UserVO info(@PathVariable String username) {
        System.out.println("即将查询 用户名 = " + username + " 的用户的信息……");
        UserVO userVO = new UserVO();
        userVO.setUsername("chengheng");
        userVO.setPassword("1234567890");
        userVO.setEmail("chengheng@qq.com");
        return userVO;
    }
}
